package com.weiaibenpao.demo.chislim.presenter;

import com.orhanobut.logger.Logger;
import com.weiaibenpao.demo.chislim.base.BasePresenter;
import com.weiaibenpao.demo.chislim.bean.Comment;
import com.weiaibenpao.demo.chislim.bean.MomentDetailsBean;
import com.weiaibenpao.demo.chislim.retrofit.RetrofitUtils;
import com.weiaibenpao.demo.chislim.retrofit.rx.LoadingTransformer;
import com.weiaibenpao.demo.chislim.retrofit.rx.NetWorkTransformer;
import com.weiaibenpao.demo.chislim.retrofit.rx.SimpleObserver;
import com.weiaibenpao.demo.chislim.view.MomentDetailsView;

/**
 * Created by Administrator on 2018/1/4.
 */

public class MomentDetailsPresenter extends BasePresenter<MomentDetailsView> {

    public void getMomentDetails(int userId,int momentId,int page){
        Logger.e("getMomentDetails----->");
        RetrofitUtils
                .getAPIService()
                .getMomentDetails(userId,momentId,page, Integer.MAX_VALUE)//由于后续需要改版，暂时不做分页，所以传一个足够大的数据过去，一次性返回所有评论
                .compose(new NetWorkTransformer<MomentDetailsBean>())
                .compose(new LoadingTransformer<MomentDetailsBean>(mContext))
                .subscribe(new SimpleObserver<MomentDetailsBean>(mCompositeDisposable) {
                    @Override
                    public void call(MomentDetailsBean bean) {
                        getView().onGetMomentDetailsSuccess(bean);
                    }

                    @Override
                    public void showErrorMsg(String message) {
                       if(getView() != null)
                        getView().onGetMomentDetailsFailed(message);
                    }
                });
    }


    public void addComment(int userId,int momentId,String commentStr){
        Logger.e("addComment----->momentId is "+momentId+"--userId is "+userId);
        RetrofitUtils
                .getAPIService()
                .addComment(userId,momentId,commentStr)
                .compose(new NetWorkTransformer<Comment>())
                .compose(new LoadingTransformer<Comment>(mContext))
                .subscribe(new SimpleObserver<Comment>(mCompositeDisposable) {
                    @Override
                    public void call(Comment bean) {
                        getView().onAddCommentSuccess(bean);
                    }

                    @Override
                    public void showErrorMsg(String message) {
                        if(getView() != null)
                            getView().onAddCommentFailed(message);
                    }
                });
    }


    public void replyComment(int userId,int parentId ,int momentId,String commentStr){
        Logger.e("replyComment----->parent id is "+parentId+"--momentId is "+momentId+"--userId is "+userId);
        RetrofitUtils
                .getAPIService()
                .replyComment(userId,parentId,momentId, commentStr)
                .compose(new NetWorkTransformer<Comment>())
                .compose(new LoadingTransformer<Comment>(mContext))

                .subscribe(new SimpleObserver<Comment>(mCompositeDisposable) {
                    @Override
                    public void call(Comment bean) {
                        getView().onReplyCommentSuccess(bean);
                    }

                    @Override
                    public void showErrorMsg(String message) {
                        if(getView() != null)
                            getView().onReplyCommentFailed(message);
                    }
                });
    }

    public void report(int userId,int targetId,int type,String content){
        Logger.e("report-----> user id "+userId+"--targetId "+targetId);
        RetrofitUtils
                .getAPIService()
                .report(userId,targetId,type,content)
                .compose(new NetWorkTransformer())
                .subscribe(new SimpleObserver(mCompositeDisposable) {
                    @Override
                    public void call(Object bean) {
                        getView().onReportSuccess();
                    }
                });
    }


    public void deleteMoment(int userId, int momentId, final int position){
        Logger.e("deleteMoment-----> user id "+userId+"--momentId "+momentId);
        RetrofitUtils
                .getAPIService()
                .deleteMoment(userId,momentId)
                .compose(new NetWorkTransformer())
                .subscribe(new SimpleObserver(mCompositeDisposable) {
                    @Override
                    public void call(Object bean) {
                        getView().onDeleteSuccess(position);
                    }
                });
    }


    public void deleteComment(int userId, final int commentId){
        Logger.e("deleteComment-----> user id "+userId+"--commentId "+commentId);
        RetrofitUtils
                .getAPIService()
                .deleteComment(userId,commentId)
                .compose(new NetWorkTransformer())
                .subscribe(new SimpleObserver(mCompositeDisposable) {
                    @Override
                    public void call(Object bean) {
                        getView().onDeleteCommentSuccess(commentId);
                    }
                });
    }

}
